package top.lcm889.nowcoder.top;

/**
 * @author Coder-lcm
 * @date 2021/2/24
 */
public class NC4_判断链表是否有环 {

    public boolean hasCycle(ListNode head) {
        ListNode node1 = head;
        ListNode node2 = head.next;
        while (node1 != null && node2 != null && node2.next != null) {
            if (node1 == node2) {
                return true;
            }
            node1 = node1.next;
            node2 = node2.next.next;
        }
        return false;
    }

    public static void main(String[] args) {

    }
}
